package com.example.demo.Entity;

import com.alibaba.excel.annotation.ExcelIgnore;
import com.alibaba.excel.annotation.ExcelProperty;
import com.alibaba.excel.metadata.BaseRowModel;
import com.fasterxml.jackson.annotation.JsonFormat;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.security.core.GrantedAuthority;
import org.springframework.security.core.authority.SimpleGrantedAuthority;
import org.springframework.security.core.userdetails.UserDetails;

import javax.persistence.*;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;

/**
 * 此处编辑人物属性类
 *
 * 要求
 * 必须有的有  主键（使用UUID 生成随机字符串） ，姓名，角色身份，创建时间（使用@column（inserting false，updating false）,最新修改时间(inserting false，updating true)）
 * 可为空的有  住址， 头像url（默认url） ，出入次数（默认0），身份证（13位字符串），年龄，昵称，微信openid
 * */

@Entity
@Table(name = "person")
@NoArgsConstructor
@AllArgsConstructor
@Data
@Builder
public class Person implements Serializable , UserDetails {

    public static final long serialVersionUID = 1L;

    @Id
    @GeneratedValue(strategy  =  GenerationType.IDENTITY)
    @ExcelProperty(value = "索引", index = 0)
    private Long Id;
    @ExcelProperty(value = "角色", index = 1)
    @Column(name = "role")
    private String role;
    @Column(name = "username")
    @ExcelProperty(value = "用户名", index = 2)
    private String username;
    @Column(name = "password")
    @ExcelProperty(value = "密码", index = 3)
    private String password;
    @Column(name = "realName")
    @ExcelProperty(value = "真实姓名", index = 4)
    private String realName;
    @Column(name = "openId")
    @ExcelProperty(value = "微信号", index = 5)
    private String openId;
    @Column(name = "area")
    @ExcelProperty(value = "居住地", index = 6)
    private String area;
    @Column(name = "createdTime",updatable = false)
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @ExcelProperty(value = "创建时间", index = 7)
    private Date createdTime =new Date();
    @Column(name = "updatedTime")
    @JsonFormat(
            pattern = "yyyy-MM-dd HH:mm:ss"
    )
    @ExcelProperty(value = "更新时间", index = 8)
    private Date updatedTime = new Date();
    @Column(name = "avatar")
    @ExcelProperty(value = "头像地址", index = 9)
    private String avatar;
    @Column(name = "is_health")
    @ExcelProperty(value = "健康状况", index = 10)
    private Boolean isHealth = true;
    @Column(name = "enabled")
//    @ExcelIgnore
    private boolean enabled=true;
    @Column(name = "accountNonExpired")
//    @ExcelIgnore
    private boolean accountNonExpired=true;
    @Column(name = "credentialsNonExpired")
//    @ExcelIgnore
    private boolean credentialsNonExpired=true;
    @Column(name = "accountNonLocked")
//    @ExcelIgnore
    private boolean accountNonLocked=true;

    @Override
    public Collection<? extends GrantedAuthority> getAuthorities() {
        List<GrantedAuthority> authorities = new ArrayList<>();

        authorities.add( new SimpleGrantedAuthority( role ) );

        return authorities;
    }

    @Override
    public String getUsername() {
        return username;
    }

    @Override
    public boolean isAccountNonExpired() {
        return accountNonExpired;
    }

    @Override
    public boolean isAccountNonLocked() {
        return accountNonLocked;
    }

    @Override
    public boolean isCredentialsNonExpired() {
        return credentialsNonExpired;
    }

    @Override
    public boolean isEnabled() {
        return enabled;
    }

}
